package animix.blue.server.user.service.impl;

import animix.blue.server.common.bean.pojo.Result;
import animix.blue.server.common.bean.model.Right;
import animix.blue.server.common.bean.tool.PagedList;
import animix.blue.server.common.bean.tool.Pagination;
import animix.blue.server.user.mapper.RightMapper;
import animix.blue.server.user.service.RightService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RightServiceImpl implements RightService {

    @Autowired
    private RightMapper rightMapper;

    @Override
    public Result list(Map<String, Object> para) {
        // 分页插件
        Integer currentPage = para.get("currentPage") == null ? 1 : Integer.parseInt((String) para.get("currentPage"));
        Integer pageSize = para.get("pageSize") == null ? 10 : Integer.parseInt((String) para.get("pageSize"));
        PageHelper.startPage(currentPage, pageSize);
        // 查询
        List<Right> list = rightMapper.list(para);
        // 分页信息
        PageInfo pageInfo = new PageInfo(list);
        Pagination pagination = new Pagination();
        pagination.setCurrent(currentPage);
        pagination.setPageSize(pageSize);
        pagination.setTotal(pageInfo.getTotal());
        // 包装
        PagedList<Right> pagedList = new PagedList();
        pagedList.setList(list);
        pagedList.setPagination(pagination);
        // 返回
        return Result.ok(pagedList);
    }
}
